package com.lvhx.springboot.utils;


import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.CollectionUtils;

import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
//import org.testng.annotations.Test;
public class BigdecimalUtil {

//    @Test
    public void test() {
//        List<Friend> friends = null;
//        friends = Arrays.asList(new Friend("A", new BigDecimal("1.3245")), new Friend("B", new BigDecimal("11.434353")), new Friend("A", new BigDecimal("2.35454")));
////        Dog zhangsan = new Dog("zhangsan", new BigDecimal("2.567"), friends);
////
////        setScaleUP(zhangsan,2);
////
////        System.out.println(zhangsan);
//        friends = null;
//        System.out.println(friends);

        List<String> list = null;
        List list1 = Arrays.asList("A", "B");
        list = list1;
        list.add("C");

        list = list1;

        System.out.println(list);


    }


    public static void setScaleUP(Object source, int newScale) {
        BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            BigDecimal ret = new BigDecimal(0);
            System.out.println(pd.getPropertyType() + "------");
            if (pd.getPropertyType().equals(BigDecimal.class)) {
                if (null != src.getPropertyValue(pd.getName())) {
                    Object value = src.getPropertyValue(pd.getName());
                    if (value instanceof BigDecimal) {
                        ret = (BigDecimal) value;
                    } else if (value instanceof String) {
                        ret = new BigDecimal((String) value);
                    } else if (value instanceof BigInteger) {
                        ret = new BigDecimal((BigInteger) value);
                    } else if (value instanceof Number) {
                        ret = new BigDecimal(((Number) value).doubleValue());
                    }
                    src.setPropertyValue(pd.getName(), new BigDecimal(new DecimalFormat("0.00#").format(ret.setScale(newScale, BigDecimal.ROUND_HALF_UP))));
                }
            } else if (pd.getPropertyType().equals(List.class)) {
                if (null != src.getPropertyValue(pd.getName())) {
                    Object value = src.getPropertyValue(pd.getName());
                    if (value instanceof List) {
                        if (!CollectionUtils.isEmpty((Collection<?>) value)) {
                            ((List) value).forEach(v -> {
                                setScaleUP(v, newScale);

                            });
                        }
                    }
                }
            }
        }
    }


    class Dog {
        private String name;
        private BigDecimal price;
        private List<Friend> friends;

        public Dog(String name, BigDecimal price, List<Friend> friends) {
            this.name = name;
            this.price = price;
            this.friends = friends;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }

        public List<Friend> getFriends() {
            return friends;
        }

        public void setFriends(List<Friend> friends) {
            this.friends = friends;
        }

        @Override
        public String toString() {
            return "Dog{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", friends=" + friends +
                    '}';
        }
    }

    class Friend {
        private String name;
        private BigDecimal price;

        public Friend(String name, BigDecimal price) {
            this.name = name;
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }

        @Override
        public String toString() {
            return "Friend{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    '}';
        }
    }

}
